<template>
  <div class="app-container">
    <el-tree :data="treePoollList" node-key="id" :props="defaultProps" default-expand-all :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span class="name">{{ node.label.nodeName }} {{ node.label.reuseTimes === 1 ? '独享' : '共享' }}</span>
        <span>
          <template v-if="node.label.parentId == '0'">
            <!-- <el-button icon="el-icon-circle-plus" type="primary" size="mini"
              @click="append(data, node.label)">新增</el-button> -->
            <i class="el-icon-circle-plus" @click="append(data, node.label)"></i>
          </template>
          <template v-else>
            <i class="iconHide el-icon-circle-plus" @click="append(data, node.label)"></i>
            <i class="iconHide el-icon-edit" @click="modify(data, node.label)"></i>
            <i class="iconHide el-icon-close" @click="remove(data, node.label)"></i>
            <template v-for="item in node.label.serviceNodePoollabelVoList">
              <i class="clickbtn" v-if="true" :key="item.labelId">{{ item.labelName }}</i>
              <!-- <el-button type="info" plain v-if="true" :key="item.labelId" size="mini">{{ item.labelName }}</el-button> -->
            </template>
            <i class="infobtn" @click="editIp(data, node.label)">IP管理({{ node.label.nodePoolipNum }})</i>
            <i class="infobtn" @click="editTag(data, node.label)">标签管理</i>
            <!-- <el-button size="mini" @click="() => append(data)">IP管理</el-button>
            <el-button size="mini" @click="() => remove(node, data)">新增标签</el-button>-->
            <!-- <el-button size="mini" type="primary" @click="() => remove(node, data)">标签管理</el-button> -->
          </template>
        </span>
      </span>
    </el-tree>

    <!-- <el-table v-loading="loading" :data="poolList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="修改人" align="center" prop="updateUser" />
      <el-table-column label="是否删除 1-是 0-否" align="center" prop="isDel" />
      <el-table-column label="状态：0 禁用  1 启用" align="center" prop="status" />
      <el-table-column label="父类线路id，顶级默认为0" align="center" prop="parentId" />
      <el-table-column label="级别：1：一级；2：二级；3：三级" align="center" prop="level" />
      <el-table-column label="节点池名称" align="center" prop="nodeName" />
      <el-table-column label="节点池线路编号" align="center" prop="nodeCode" />
      <el-table-column label="节点池类型" align="center" prop="nodeType" />
      <el-table-column label="挂载ip数量" align="center" prop="ipNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:pool:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:pool:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table> -->

    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" /> -->
    <el-dialog :title="title" :visible.sync="addNodePool" width="500px" class="addNodePool" append-to-body
      :close-on-click-modal="false">
      <AddNodePool v-if="addNodePool" :fromData="nodePoolData" @subPoolDate="subPoolDate"></AddNodePool>
    </el-dialog>

    <el-dialog title="IP管理" :visible.sync="IPOpen" width="680px" class="ipDialog" append-to-body>
      <IpManage v-if="IPOpen" :fromData="ipData" @handlerIP="handlerIP"></IpManage>
    </el-dialog>

    <el-dialog title="标签管理" :visible.sync="TagOpen" width="680px" class="ipDialog" append-to-body>
      <TagManage v-if="TagOpen" :fromData="ipData" @handlerTag="handlerTag"></TagManage>
    </el-dialog>

    <!-- 添加或修改IP智能调度对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="form.sort" type="number" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="修改人" prop="updateUser">
          <el-input v-model="form.updateUser" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="是否删除 1-是 0-否" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入是否删除 1-是 0-否" />
        </el-form-item>
        <el-form-item label="父类线路id，顶级默认为0" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父类线路id，顶级默认为0" />
        </el-form-item>
        <el-form-item label="级别：1：一级；2：二级；3：三级" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别：1：一级；2：二级；3：三级" />
        </el-form-item>
        <el-form-item label="节点池名称" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="请输入节点池名称" />
        </el-form-item>
        <el-form-item label="节点池线路编号" prop="nodeCode">
          <el-input v-model="form.nodeCode" placeholder="请输入节点池线路编号" />
        </el-form-item>
        <el-form-item label="挂载ip数量" prop="ipNumber">
          <el-input v-model="form.ipNumber" placeholder="请输入挂载ip数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listPool, treeNodePool, getPool, delPool, addPool, updatePool } from "@/api/system/pool";
import IpManage from './IpManage.vue'
import TagManage from './TagManage.vue'
import AddNodePool from './addNodePool.vue'

export default {
  name: "Pool",
  components: { AddNodePool, IpManage, TagManage },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // IP智能调度表格数据
      poolList: [],
      // 弹出层标题
      title: "",
      // 树状层级数据
      treePoollList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: null,
        createUser: null,
        updateUser: null,
        isDel: null,
        status: null,
        parentId: null,
        level: null,
        nodeName: null,
        nodeCode: null,
        nodeType: null,
        ipNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      IPOpen: false,  // IP管理
      ipData: {
        id: ''
      },
      TagOpen: false,  // 标签管理
      addNodePool: false,  // 新增IP
      nodePoolData: {
        editType: '',
        id: '',
        sort: '',
        parentId: '',
        level: '',
        nodeName: '',
        nodeType: ''
      },
      defaultProps: {
        // 这个是往下找children的，这个children就不用动了，不过可以试试，效果就是找不到data下的子节点
        children: 'children',
        // props绑定这个
        // a：data   b：node
        // 自己脑子里把下面的 a 和 b 转义一下，就明白了
        label(a, b) {
          // console.log(i++)
          // console.log('label:','data:',a,'node:',b)
          // 参考文章返回的格式直接变成了字符串
          // 因为我这里需求需要，只能返回个对象，可以参考html，A处，方便自定义一行的样式
          // 返回结果就是node中的label值，我这里是个对象
          return {
            level: a.level,
            nodeName: a.nodeName,
            reuseTimes: a.reuseTimes,
            nodeType: a.nodeType,
            parentId: a.parentId,
            id: a.id,
            serviceNodePoollabelVoList: a.serviceNodePoollabelVoList,
            servierNodePoolipVoList: a.servierNodePoolipVoList,
            status: a.status,
            nodePoolipNum: a.nodePoolipNum,
          }
        },
        // label: 'nodeName'
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询IP智能调度列表 */
    getList() {
      this.loading = true;
      treeNodePool({ status: null }).then(response => {
        this.treePoollList = response.data || [];
        this.loading = false;
      });
    },
    subPoolDate(v) {
      console.log(v);
      if (v.subType) {
        if (v.editType == 1) {
          addPool(v).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.resetPool();
            this.addNodePool = false;
            this.getList();
          });
        } else {
          updatePool(v).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.resetPool();
            this.addNodePool = false;
            this.getList();
          });
        }
      } else {
        this.nodePoolData.parentId = '';
        this.nodePoolData.level = '';
        this.addNodePool = false;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sort: null,
        updateTime: null,
        createTime: null,
        createUser: null,
        updateUser: null,
        isDel: null,
        status: null,
        parentId: null,
        level: null,
        nodeName: null,
        nodeCode: null,
        nodeType: null,
        ipNumber: null
      };
      this.resetForm("form");
    },
    // 表单重置
    resetPool() {
      this.nodePoolData = {
        editType: '',
        id: '',
        sort: '',
        parentId: '',
        level: '',
        nodeName: '',
        nodeType: ''
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加IP智能调度";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPool(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改IP智能调度";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePool(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPool(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除IP智能调度编号为"' + ids + '"的数据项？').then(function () {
        return delPool(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pool/export', {
        ...this.queryParams
      }, `pool_${new Date().getTime()}.xlsx`)
    },

    append(data) {
      this.nodePoolData.editType = 1;   // 1新增
      this.nodePoolData.parentId = data.id;
      this.nodePoolData.level = data.level + 1;
      this.addNodePool = true;
      this.title = '新增子节点池';
    },
    modify(data) {
      this.nodePoolData.editType = 2;   // 1修改
      this.nodePoolData.id = data.id;
      this.nodePoolData.sort = data.sort || '';
      this.nodePoolData.level = data.level;
      this.nodePoolData.nodeName = data.nodeName || '';
      this.nodePoolData.nodeType = data.nodeType || '';
      this.nodePoolData.parentId = data.parentId;
      this.addNodePool = true;
      this.title = '修改子节点池';
    },
    remove(data, node) {
      // const parent = node.parent;
      // const children = parent.data.children || parent.data;
      // const index = children.findIndex(d => d.id === data.id);
      // children.splice(index, 1);
      this.$modal.confirm('是否确认删除节点池"' + data.nodeName + '"的数据项？').then(function () {
        return delPool(data.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    editIp(data, node) {
      this.ipData = data;
      this.IPOpen = true;
    },
    handlerIP(v) {
      this.IPOpen = false;
    },
    editTag(data, node) {
      this.ipData = data;
      this.TagOpen = true;
    },
    handlerTag(v) {
      if (v > 0) {
        this.TagOpen = false;
        this.$router.push("/common/label");
      } else {
        this.TagOpen = false;
        this.getList();
      }
    },

  }
};
</script>
<style scoped lang="scss">
.custom-tree-node {
  padding: 2px 0;

  span {
    vertical-align: middle;
  }

  .name {
    margin-right: 10px;
  }

  i {
    vertical-align: middle;
    margin-right: 4px;
  }

  .infobtn {
    padding: 5px 15px;
    border: 1px solid #d3d4d6;
    border-radius: 4px;
    font-size: 12px;
    line-height: 16px;
    font-style: normal;
    background-color: #f4f4f5;
    color: #909399;
    margin-right: 4px;

    &.active {
      border: 1px solid #409eff;
      background-color: #409eff;
      color: #FFFFFF;
    }
  }

  .clickbtn {
    padding: 5px 15px;
    border: 1px solid #409eff;
    border-radius: 4px;
    font-size: 12px;
    line-height: 16px;
    font-style: normal;
    background-color: #409eff;
    color: #FFFFFF;
    margin-right: 4px;
  }

}

.el-tree-node__content {
  height: auto;
  padding: 3px 0;
}
</style>